/* 
 * File:   File.c
 * Author: Muhammad Anis
 */

#include <stdio.h>

#include "File.h"
/**
 * Read content of a file
 * @param filename file name
 * @return content of file
 */
char* readContent(const char* filename){
    char* fcontent = NULL;
    long fsize = 0;
    FILE *fp;
    
    fp = fopen(filename,"rb");
    if(fp){
        fseek(fp,0L,SEEK_END);
        fsize = ftell(fp);
        rewind(fp);
        fcontent = (char*) malloc(sizeof(char)*(fsize+1));
        fread(fcontent,1,fsize,fp);        
        fcontent[fsize]='\0';
        fclose(fp);
    }else{
        printf("File Not Found\n");
    }
    return fcontent;
}
/**
 * Save file 
 * @param content file content
 * @param filename file name
 */
void SaveFile(char* content, const char* filename){
    FILE *fp;
    fp = fopen(filename,"wb+");
    if(fp){
        // printf("Length of string : %d",GetCSStringLength(content));
        fwrite(content,sizeof(char),GetCSStringLength(content),fp);
        fclose(fp);
    }else{
        printf("File Not Found\n");
    }
}
/**
 * Append content to file
 * @param content file content
 * @param filename file name
 */
void AppendFile(char* content, const char* filename){
    FILE *fp;
    fp = fopen(filename,"a+");
    if(fp){
        fprintf(fp,content);
        fclose(fp);
    }else{
        printf("File Not Found\n");
    }
}

